WebWork 2 : 简单校验器
This page last changed on Jul 10, 2004 by unkyaku.
下列校验器包含在缺省的validators.xml中:
注意: 如果你使用自己的validators.xml文件, 上述名字可以修改. requiredSimpleAction-validation.xml:<validators> <field name="bar"> <field-validator type="required"> <message>You must enter a value for bar.</message> </field-validator> </field> </validators> requiredstringLoginAction-validation.xml: <validators> <field name="userName"> <field-validator type="requiredstring"> <message>You must enter an username.</message> </field-validator> </field> </validators> The error is shown if request parameter userName is missing or an empty string #topint<validators> <field name="foo"> <field-validator type="int"> <param name="min">0</param> <param name="max">100</param> <message key="foo.range">Could not find foo.range!</message> </field-validator> </field> </validators> date<validators> <field name="startDate"> <field-validator type="date"> <param name="min">12/22/2002</param> <param name="max">12/25/2002</param> <message>The date must be between 12-22-2002 and 12-25-2002.</message> </field-validator> </field> </validators> expression<validators> <validator type="expression"> <param name="expression">foo > bar</param> <message>Foo must be greater than Bar. Foo = ${foo}, Bar = ${bar}.</message> </validator> </validators> 校验器不仅检查一个字段. 如果表达式中包含xml字符, 可以使用CDATA. #topfieldexpression<validators> <field name="productCode"> <field-validator type="fieldexpression"> <param name="expression">name.length() == 5</param> <message>Product code must be 5 characters, it is currently '${productCode}'</message> </field-validator> </field> </validators> <validators> <field name="email"> <field-validator type="email"> <message>You must enter a valid email address.</message> </field-validator> </field> </validators> address必须符合格式xxx@yyy.com|net|gov|org|edu|info|mil|biz|tv|… #topurl<validators> <field name="companyUrl"> <field-validator type="url"> <message>You must enter a valid URL.</message> </field-validator> </field> </validators> |
Document generated by Confluence on Dec 14, 2004 16:36 |